package com.zt.teacher.service.impl;

import com.zt.common.pojo.exam.Exam;
import com.zt.common.pojo.exam.ExamAnswer;
import com.zt.common.pojo.exam.ExamQuestionBank;
import com.zt.common.utils.RedisUtil;
import com.zt.teacher.dao.ExamDao;
import com.zt.teacher.dto.ExamSummaryDTO;
import com.zt.teacher.dto.SummaryDTO;
import com.zt.teacher.service.ActiveClassService;
import com.zt.teacher.service.ClassSelectionService;
import com.zt.teacher.service.ExamAnswerService;
import com.zt.teacher.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * description:
 * author:zt
 * date:2022-01-17
 */
@Transactional
@Service
public class ExamServiceImpl implements ExamService {

    @Autowired
    private ExamDao examDao;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ClassSelectionService classSelectionService;
    @Autowired
    private ExamAnswerService examAnswerService;

    @Override
    public int insert(Exam exam){
        exam.setCreateTime(new Date());
        examDao.insert(exam);

        //关联 标识正在考试
        redisUtil.set(exam.getClassId() + "-exam",exam.getId());
        return exam.getId();
    }

    /**
     * @param classId 班级id
     * @return 获取该班级所有考试情况
     */
    @Override
    public ArrayList<ExamSummaryDTO> getExamSituation(int classId){
        ArrayList<ExamSummaryDTO> res = new ArrayList<>();
        //根据班级id获取该班级发布的所有考试
        List<Exam> examList = examDao.getByClassId(classId);
        //获取该班级的所有学生id
        List<String> studentIds = classSelectionService.getStuIdByClassId(classId);
        if (studentIds == null || studentIds.size()==0) return res;
        //遍历每场考试获得每场考试的平均分情况
        for (Exam exam : examList) {
            double totalScore = 0;
            for (String s : studentIds)
                totalScore += getScore(s,exam.getId());

            res.add(new ExamSummaryDTO(exam.getId(),exam.getName(),exam.getCreateTime(),totalScore/studentIds.size()));
        }
        return res;
    }

    @Override
    public List<Exam> getExamByClassId(int classId){
        return examDao.getByClassId(classId);
    }

    @Override
    public List<ExamQuestionBank> getQuestionByExamId(int examId){
        return examDao.getQuestionByExamId(examId);
    }

    @Override
    public double getScore(String studentId, int examId){
        //根据考试id获取该场考试的所有试题情况
        List<ExamQuestionBank> questions = examDao.getQuestionByExamId(examId);
        //获取试题id
        List<Integer> questionIds = questions.stream().map(ExamQuestionBank::getId).collect(Collectors.toList());
        //获取回答
        List<ExamAnswer> answers = examAnswerService.getAnswerByQuestionIdsAndStuId(studentId, questionIds,examId);
        if (answers== null || answers.size()==0) return 0;

        HashMap<Integer, ExamAnswer> answerMap = new HashMap<>();
        answers.forEach(a -> answerMap.put(a.getQuestionId(),a));
        //对答案进行比对计算这位同学的分数
        double correct = 0;
        for (ExamQuestionBank question : questions) {
            if (!answerMap.containsKey(question.getId())) continue;
            if (answerMap.get(question.getId()).getAnswer().equals(question.getStandardAnswer())) correct++;
        }

        return (correct / questions.size()) * 100;
    }
}
